package com.test.system.admin.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author 徒有琴
 */
public class QuartzUtil {
    private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();

    public static void addJob(String jobName, String jobGroup, String triggerName, String triggerGroup, String cron, Class jobClass) {
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerName, triggerGroup)
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .startNow().build();//现在开始执行
        JobDetail detail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroup).build();

        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.scheduleJob(detail, trigger);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public static void removeJob(String jobName, String jobGroup, String triggerName, String triggerGroup) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
            scheduler.pauseTrigger(triggerKey);
            scheduler.unscheduleJob(triggerKey);
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroup));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public static void updateJob(String triggerName, String triggerGroup, String cron) {
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
            CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            String oldCron = cronTrigger.getCronExpression();
            if (!oldCron.equals(cron)) {//和原来的表达式不一样
                //创建新的触发器
                Trigger trigger = TriggerBuilder.newTrigger()
                        .withIdentity(triggerName, triggerGroup)
                        .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                        .startNow().build();//现在开始执行
                scheduler.rescheduleJob(triggerKey, trigger);
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public static void pauseJob(String jobName,String jobGroup) {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
            // sched.pauseJob(jobName, JOB_GROUP_NAME);
            sched.pauseJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    public static void resumeJob(String jobName,String jobGroup) {
        try {
            Scheduler sched = schedulerFactory.getScheduler();
            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
            // sched.resumeJob(jobName, JOB_GROUP_NAME);
            sched.resumeJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }

    public static Integer getJobStatus(String jobName,String jobGroup) {

        try {
            Scheduler sched = schedulerFactory.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
            // return sched.getTriggerState(jobName, TRIGGER_GROUP_NAME);
            Trigger.TriggerState state = sched.getTriggerState(triggerKey);
            return state.ordinal();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        int unknowStatus = -7;
        return unknowStatus;
    }
}